package com.ellwood.modules.system.logic.user;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ellwood.common.core.base.BaseLogic;
import com.ellwood.common.core.enums.HttpCodeEnum;
import com.ellwood.common.core.exception.SystemException;
import com.ellwood.common.core.utils.EllStrUtil;
import com.ellwood.dataMapper.system.entity.UserEntity;
import com.ellwood.dataMapper.system.mapper.UserMapper;
import com.ellwood.modules.system.convert.UserConvert;
import com.ellwood.modules.system.domain.VO.user.UserEditVO;
import com.ellwood.modules.system.domain.VO.user.UserPageQueryVO;
import com.ellwood.modules.system.domain.VO.user.UserQueryVo;
import com.ellwood.modules.system.domain.VO.user.UserSaveVo;
import lombok.AllArgsConstructor;

import java.util.HashMap;
import java.util.Map;


/**
 * @ProjectName: Ellwood-framework-boot
 * @PackageName: com.ellwood.modules.system.logic.user
 * @ClassName: UserLogic
 * @Description: // 用户逻辑业务处理
 * @Author: Ellwood
 * @CreateDate: 2024/7/22 - 下午1:59
 * @Version: V1.0.0
 */

@AllArgsConstructor
public class UserLogic extends BaseLogic<UserEntity> {

    private final UserMapper userMapper;

    /**
     * 获取用户查询包装器
     *
     * @return 返回wrapper
     */
    public static Wrapper<UserEntity> getWrapper(UserQueryVo queryVo) {
        LambdaQueryWrapper<UserEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(
                EllStrUtil.isNotBlank(queryVo.getAccount()),
                UserEntity::getAccount,
                queryVo.getAccount()
        ).like(

                EllStrUtil.isNotBlank(queryVo.getNickName()),
                UserEntity::getNickName,
                queryVo.getNickName()
        );
        return wrapper;
    }

    /**
     * 获取查询参数
     *
     * @param queryVo 查询参数
     * @return 返回参数
     */
    public static Map<String, Object> getParams(UserPageQueryVO queryVo) {
        Map<String, Object> params = new HashMap<>();
        params.put("account", queryVo.getAccount());
        params.put("nickName", queryVo.getNickName());
        params.put("age", queryVo.getAge());
        params.put("gender", queryVo.getGender());
        params.put("email", queryVo.getEmail());
        params.put("phone", queryVo.getPhone());

        return params;
    }

    /**
     * 新增用户业务处理
     *
     * @param userVo 用户vo
     * @return 返回用户实体
     */
    public UserEntity getAddUserEntity(UserSaveVo userVo) {
        // 新增密码不能为空
        if (EllStrUtil.isBlank(userVo.getPassword())) {
            throw new SystemException(HttpCodeEnum.PASSWORD_NOT_NULL);
        }
        // 将传入的vo对象转成entity
        UserEntity userEntity = UserConvert.INSTANCE.convertSaveEntity(userVo);
        //检查账号是否重复
        long rustleAccount = userMapper.selectCount(
                new LambdaQueryWrapper<UserEntity>().eq(UserEntity::getAccount, userEntity.getAccount()));
        if (rustleAccount > 0) throw new SystemException(HttpCodeEnum.USERNAME_EXIST);

        //检查手机号是否重复
        long resultPhone = userMapper.selectCount(
                new LambdaQueryWrapper<UserEntity>().eq(UserEntity::getPhone, userEntity.getPhone()));
        if (resultPhone > 0) throw new SystemException(HttpCodeEnum.PHONE_NUMBER_EXIST);

        return userEntity;
    }

    /**
     * 编辑用户业务处理
     *
     * @param userVo 用户vo
     * @return 返回用户实体
     */
    public UserEntity getEditUserEntity(UserEditVO userVo) {
        // 将传入的vo对象转成entity
        UserEntity userEntity = UserConvert.INSTANCE.convertEditEntity(userVo);

        // 查找用户是否存在
        UserEntity user = userMapper.selectById(userVo.getUserId());
        if (user == null) {
            throw new SystemException(HttpCodeEnum.QUERY_DATA_NULL);
        } else if (!user.getUserId().equals(userEntity.getUserId())) {
            throw new SystemException(HttpCodeEnum.QUERY_DATA_NOT_EXIST);
        }

        // 校验要修改的账号是否已存在
        Long account = userMapper.selectCount(
                new LambdaQueryWrapper<UserEntity>().eq(UserEntity::getAccount, userEntity.getAccount()));
        if (account > 0) throw new SystemException(HttpCodeEnum.USERNAME_EXIST);

        // 校验手机号是否重复，以便后面手机号登录之用
        Long phone = userMapper.selectCount(
                new LambdaQueryWrapper<UserEntity>().eq(UserEntity::getPhone, userEntity.getPhone()));
        if (phone > 0) throw new SystemException(HttpCodeEnum.PHONE_NUMBER_EXIST);
        return userEntity;
    }

}